package auto;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.imgproc.Imgproc;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.MultiResolutionImage;

public class FindImg {

    public static void main(String[] args) {

    }

    /*
     * @Author Dongle
     * @Description 截全屏
     * @Date 22:32 2022/12/28
     * @Param
     * @return
     **/
    public static BufferedImage screenCapture() {
        Dimension tk = Toolkit.getDefaultToolkit().getScreenSize();
        return screenCapture(new Rectangle(tk));

    }

    public static BufferedImage screenCapture(Rectangle rectangle) {
        Robot robot;
        try {
            robot = new Robot();
        } catch (AWTException e) {
            throw new RuntimeException(e);
        }
//        BufferedImage img = robot.createScreenCapture(rectangle);
        MultiResolutionImage mrImage = robot.createMultiResolutionScreenCapture(rectangle);
        // 多屏结果
        java.util.List<Image> resolutionVariants = mrImage.getResolutionVariants();
        BufferedImage img;
        if (resolutionVariants.size() > 1) {
            img = (BufferedImage) resolutionVariants.get(1);
        } else {
            img = (BufferedImage) resolutionVariants.get(0);
        }
        return img;

    }

    public void run(BufferedImage findImage) {
        // load the native OpenCV library
//        nu.pattern.OpenCV.loadLocally();
        BufferedImage screenCapture = screenCapture();
        Mat screenCaptureMat = bufferedImageToMat(screenCapture);
        Mat findImageMat = bufferedImageToMat(findImage);
        Point res = matchingMethod(screenCaptureMat, findImageMat);
        if (res != null) {
            System.out.println(res.x + "   " + res.y);
            mouseMove((int) res.x, (int) res.y);
        }
    }

    private Point matchingMethod(Mat screenCapture, Mat templ) {
        System.out.println("开始找图");
        Mat img_idsplay = new Mat();
        screenCapture.copyTo(img_idsplay);
        int match_method = Imgproc.TM_SQDIFF_NORMED;
        int result_cols = screenCapture.cols() - templ.cols() + 1;
        int result_rows = screenCapture.rows() - templ.rows() + 1;
        Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
//        if (use_mask) {
//            Imgproc.matchTemplate(img, templ, result, Imgproc.TM_CCOEFF_NORMED, mask);
//        } else {
        Imgproc.matchTemplate(screenCapture, templ, result, match_method);
        //! [normalize]
        Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
        Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
        Point matchLoc;
        matchLoc = mmr.minLoc;
        return matchLoc;
//        System.out.println("minVal 为" + mmr.minVal);
//        if (2E-10 > mmr.minVal && mmr.minVal > 1E-10) {
//            System.out.println("找到图片");
//            return matchLoc;
//        }
//        System.out.println("没找到图片");
//        return null;
    }

    public Mat bufferedImageToMat(BufferedImage image) {
        BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g2d = newImage.createGraphics();
        g2d.drawImage(image, 0, 0, null);
        g2d.dispose();
        Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8U);
        byte[] data = ((DataBufferByte) newImage.getRaster().getDataBuffer()).getData();
        mat.put(0, 0, data);
        return mat;
    }

    public void mouseMove(int x, int y) {
        try {
            Robot robot = new Robot();
            robot.mouseMove(x, y);
        } catch (AWTException e) {
            throw new RuntimeException(e);
        }
    }
}



